<template>
  <div class='container'>
     <el-dialog  :title="showText" :visible="showDialog"  @close="btnCancel" >
         <el-form  label-width="120px" :model="data1"   :rules="rules"  ref="perForm">
      <el-form-item label="学科名称" prop="subjectName">
        <el-input style="width:50%" v-model="data1.subjectName" placeholder="请输入姓名" />
      </el-form-item>
       <el-form-item label="是否显示"  prop="isFrontDisplay">
         <el-switch
            v-model="data1.isFrontDisplay"
          active-color="#13ce66"
         inactive-color="#ff4949"
          />
        </el-form-item>
         </el-form>
         <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="isok">确定</el-button>
        </el-col>
      </el-row>
    </template>
      </el-dialog>
  </div>
</template>

<script>
import { update, add } from '@/api/hmmm/subjects'
export default {
  name: 'subjects-add',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    data1: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      rules: {
        subjectName: { required: true, message: '请输入学科名称', trigger: 'blur' }
      }
    }
  },
  methods: {
    // 取消
    btnCancel () {
      this.data1.subjectName = ''
      this.$refs.perForm.resetFields()
      this.$emit('update:showDialog', false)
    },

    // 确定按钮
    async  isok () {
      if (this.data1.isFrontDisplay === true) {
        this.data1.isFrontDisplay = 1
      } else {
        this.data1.isFrontDisplay = 0
      }
      try {
        await this.$refs.perForm.validate()
        if (this.data1.id) {
          await update(this.data1)
        } else {
          await add(this.data1)
        }
        this.$parent.list1()
        this.$message.success('操作成功')
        this.$emit('update:showDialog', false)
        this.data1.subjectName = ''
      } catch (error) {
        console.log(error)
      }
    }

  },
  computed: {
    showText () {
      return this.data1.id ? '编辑' : '新增'
    }

  }

}
</script>

<style scoped lang='less'></style>
